
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports _2bMatesSite.Common

Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for GroupBands.
	''' </summary>
	Public Class GroupBands
		'
		' Members
		'

		Protected id As Integer
		Protected groupID As Integer
		Protected name As String
		Protected members As String
		Protected influences As String
		Protected soundsLike As String
		Protected genre1 As Integer
		Protected genre2 As Integer
		Protected genre3 As Integer
		Protected siteURL As String
		Protected recordLabel As String
		Protected labelType As Integer

		'
		' GroupBands constructors
		'

		Public Sub New(ByVal grpID As Integer)
			Init(grpID)
		End Sub

		' Loads group from a data reader
		Public Sub New(ByVal dr As SqlDataReader)
			LoadFromReader(dr)
		End Sub

		'
		' Properties
		'

		Public ReadOnly Property ID() As Integer
			Get
				Return id
			End Get
		End Property

		Public Property GroupID() As Integer
			Get
				Return groupID
			End Get
			Set
				groupID = value
			End Set
		End Property

		Public Property Name() As String
			Get
				Return name
			End Get

			Set
				name = value
			End Set
		End Property

		Public Property Members() As String
			Get
				Return members
			End Get

			Set
				members = value
			End Set
		End Property

		Public Property Influences() As String
			Get
				Return influences
			End Get

			Set
				influences = value
			End Set
		End Property

		Public Property SoundsLike() As String
			Get
				Return soundsLike
			End Get

			Set
				soundsLike = value
			End Set
		End Property

		Public Property Genre1() As Integer
			Get
				Return genre1
			End Get
			Set
				genre1 = value
			End Set
		End Property


		Public Property Genre2() As Integer
			Get
				Return genre2
			End Get
			Set
				genre2 = value
			End Set
		End Property

		Public Property Genre3() As Integer
			Get
				Return genre3
			End Get
			Set
				genre3 = value
			End Set
		End Property

		Public Property SiteURL() As String
			Get
				Return siteURL
			End Get

			Set
				siteURL = value
			End Set
		End Property

		Public Property RecordLabel() As String
			Get
				Return recordLabel
			End Get

			Set
				recordLabel = value
			End Set
		End Property

		Public Property LabelType() As Integer
			Get
				Return labelType
			End Get
			Set
				labelType = value
			End Set
		End Property

		'
		' Public methods
		'

		' Save the modifications to the groupbads made so far
		' If it's a new groupbands inserts it into the database
		Public Sub Save()
			If id < 0 Then
				DoInsert()
			Else
				DoUpdate()
			End If
		End Sub

		' Find a groupbands by groupID
		' returns null if there is no such groupbands was not found
		Public Shared Function GetGroupBandsByGroupID(ByVal grpID As Integer) As GroupBands
			Dim cmd As New SqlCommand("SELECT * FROM GroupBands WHERE GroupID=@grpID")
			cmd.Parameters.Add("@grpID", grpID)

			Return GetGroupBandsByCmd(cmd)
		End Function

		'
		' Private and protected methods
		'

		Protected Sub Init(ByVal grpID As Integer)
			id = -1
			groupID = grpID
			name = ""
			members = ""
			influences = ""
			soundsLike = ""
			genre1 = -1
			genre2 = -1
			genre3 = -1
			siteURL = ""
			recordLabel = ""
			labelType = -1
		End Sub

		Protected Sub LoadFromReader(ByVal dr As SqlDataReader)
			id = dr.GetInt32(dr.GetOrdinal("ID"))

			groupID = dr.GetInt32(dr.GetOrdinal("GroupID"))

			name = DBManager.GetNZString(dr, "Name")

			members = DBManager.GetNZString(dr, "Members")

			influences = DBManager.GetNZString(dr, "Influences")

			soundsLike = DBManager.GetNZString(dr, "SoundsLike")

			genre1 = DBManager.GetNZInt32(dr, "Genre1")

			genre2 = DBManager.GetNZInt32(dr, "Genre2")

			genre3 = DBManager.GetNZInt32(dr, "Genre3")

			siteURL = DBManager.GetNZString(dr, "SiteURL")

			recordLabel = DBManager.GetNZString(dr, "RecordLabel")

			labelType = DBManager.GetNZInt32(dr, "LabelType")

		End Sub

		Protected Sub DoInsert()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				' Insert the group
				Dim cmd As New SqlCommand(GetInsertSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				PopulateCommand(cmd)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Sub DoUpdate()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As New SqlCommand(GetUpdateSQL())
				cmd.Connection = conn
				cmd.Transaction = tran

				PopulateCommand(cmd)

				cmd.Parameters.Add("@ID", id)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)

				tran.Commit()
			Catch e As Exception
				tran.Rollback()
				Throw e
			Finally
				conn.Close()
			End Try
		End Sub

		Private Shared Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO GroupBands" + Environment.NewLine
			sql += "( GroupID, Name, Members, Influences, SoundsLike, Genre1, Genre2, Genre3, " + "SiteURL, RecordLabel, LabelType )" + Environment.NewLine
			sql += "VALUES" + Environment.NewLine
			sql += "( @GroupID, @Name, @Members, @Influences, @SoundsLike, @Genre1, @Genre2, @Genre3, " + "@SiteURL, @RecordLabel, @LabelType )" + Environment.NewLine
			Return sql
		End Function

		Private Shared Function GetUpdateSQL() As String
			Dim sql As String = "UPDATE GroupBands SET" + Environment.NewLine
			sql += "" & Chr(9) & "GroupID=@GroupID," + Environment.NewLine
			sql += "" & Chr(9) & "Name=@Name," + Environment.NewLine
			sql += "" & Chr(9) & "Members=@Members," + Environment.NewLine
			sql += "" & Chr(9) & "Influences=@Influences," + Environment.NewLine
			sql += "" & Chr(9) & "SoundsLike=@SoundsLike," + Environment.NewLine
			sql += "" & Chr(9) & "Genre1=@Genre1," + Environment.NewLine
			sql += "" & Chr(9) & "Genre2=@Genre2," + Environment.NewLine
			sql += "" & Chr(9) & "Genre3=@Genre3," + Environment.NewLine
			sql += "" & Chr(9) & "SiteURL=@SiteURL," + Environment.NewLine
			sql += "" & Chr(9) & "RecordLabel=@RecordLabel," + Environment.NewLine
			sql += "" & Chr(9) & "LabelType=@LabelType" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "ID=@ID" + Environment.NewLine
			Return sql
		End Function

		' Retrieve the groupbands using the given SQL command object
		' return init groupbands if no group bands found
		' NOTE: asserts that only one groupbands is selected
		'		for more groupbands use another method
		Protected Shared Function GetGroupBandsByCmd(ByVal cmd As SqlCommand) As GroupBands
			Dim grpBands As GroupBands = Nothing

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				If dr.Read() Then
					grpBands = New GroupBands(dr)
					Debug.Assert(Not dr.Read())
				End If

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return grpBands
		End Function

		' Populates the command with the groupbands's properties
		' NOTE: skips the ID property, since it's need only on insert
		Private Sub PopulateCommand(ByVal cmd As SqlCommand)
			cmd.Parameters.Add("@GroupID", groupID)
			DBManager.SetNZValue(cmd, "@Name", name)
			DBManager.SetNZValue(cmd, "@Members", members)
			DBManager.SetNZValue(cmd, "@Influences", influences)
			DBManager.SetNZValue(cmd, "@SoundsLike", soundsLike)
			DBManager.SetNZValue(cmd, "@Genre1", genre1)
			DBManager.SetNZValue(cmd, "@Genre2", genre2)
			DBManager.SetNZValue(cmd, "@Genre3", genre3)
			DBManager.SetNZValue(cmd, "@SiteURL", siteURL)
			DBManager.SetNZValue(cmd, "@RecordLabel", recordLabel)
			DBManager.SetNZValue(cmd, "@LabelType", labelType)
		End Sub
	End Class
End Namespace






